package com.xiaoshi.utils;

import com.xiaoshi.system.entity.oauth.ClientDetails;
import com.xiaoshi.system.entity.user.PcUser;
import com.xiaoshi.system.entity.user.User;
import com.xiaoshi.interfaces.ConstantsRedis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;

@Slf4j
public class CurrentUserUtils {
    @Autowired
    private static RedisUtils redisUtils;

    /**
     * 获取用户信息
     *
     * @return MyResUser 当前用户信息
     */
    public static User getCurrentUser() {
        try {
            LinkedHashMap<String, Object> authenticationDetails = getAuthenticationDetails();
            // 是不是客户端登入
            Boolean isclient = (Boolean) authenticationDetails.get("clientOnly");
            Object principal = authenticationDetails.get("principal");
            Object userType = authenticationDetails.get("userType");
            if(ObjectUtils.equals(userType,-1)){
                return clientUser(principal.toString());
            }
            return pcUser(principal);
        } catch (Exception e) {
            log.error("获取当前用户信息失败", e);
            return null;
        }
    }


    private  static User pcUser(Object principal){
        PcUser user = GsonUtils.myGsonToBean(principal, PcUser.class);
        user.setOrgUsername(user.getUsername());
        user.setOrgJobNameOrRealName(user.getJobName());
        user.setOrgJobNo(user.getJobNo());
        user.setOrgDeptName(user.getDeptName());
        user.setOrgRoleName(user.getRoleName());
        user.setOrgFid(String.valueOf(user.getFid()));
        return user;
    }



    /**
     * 分装客户端  参数
     * @param clientName
     * @return
     */
    private static User  clientUser(String clientName){
        User user = new PcUser();
        user.setOrgUsername(clientName);
        user.setOrgJobNameOrRealName(clientName);
        user.setOrgJobNo(clientName);
        user.setOrgDeptName("接口部");
        user.setOrgRoleName("接口");
        List<ClientDetails> clientDetailsList = redisUtils.lGetAll(ConstantsRedis.SysKey.SYS_CLIENDS_FIND_ALL_, ClientDetails.class);
        if(ObjectUtils.isNotEmpty(clientDetailsList)) {
            ClientDetails clientDetails = clientDetailsList.stream().filter(o -> ObjectUtils.equals(o.getClientId(), clientName)).findFirst().get();
            if(ObjectUtils.isNotEmpty(clientDetails,clientDetails.getFid())) user.setOrgFid(String.valueOf(clientDetails.getFid()));
        }
        return user;
    }






    public static Object getCurrentUserObj() {
        LinkedHashMap<String, Object> authenticationDetails = getAuthenticationDetails();
        return  authenticationDetails.get("principal");

    }




    /**
     * 获取当前jobname名称
     *
     * @return String 用户名
     */
    public static String getJobName() {
        User user = getCurrentUser();
        if (ObjectUtils.isNotEmpty(user)) {
            return user.getOrgJobNameOrRealName();
        }
        return null;
    }


    /**
     * 获取当前jobname + jobNo名称
     *
     * @return String
     */
    public static String getJobNameAndJobNo() {
        User user = getCurrentUser();
        if (ObjectUtils.isNotEmpty(user)) {
            return user.getOrgJobNameOrRealName()+"/"+user.getOrgJobNo();
        }
        return null;
    }

    /**
     * 获取当前用户名称
     *
     * @return String 用户名
     */
    public static String getCurrentUsername() {
        User user = getCurrentUser();
        if (ObjectUtils.isNotEmpty(user)) {
            return user.getOrgUsername();
        }
        return (String) getOauth2Authentication().getPrincipal();
    }

    /**
     * 获取当前用户权限集
     *
     * @return Collection<GrantedAuthority>权限集
     */
    public static Collection<GrantedAuthority> getCurrentUserAuthority() {
        return getOauth2Authentication().getAuthorities();
    }


    /**
     * 通过  上下文对象
     * @return
     */
    private static OAuth2Authentication getOauth2Authentication() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return (OAuth2Authentication) authentication;
    }

    @SuppressWarnings("all")
    private static LinkedHashMap<String, Object> getAuthenticationDetails() {
        return (LinkedHashMap<String, Object>) getOauth2Authentication().getUserAuthentication().getDetails();
    }

}
